package com.ccjq.jq173.shosestore.order.dao.impl;

import com.ccjq.jq173.shosestore.core.db.manager.DbConnection;
import com.ccjq.jq173.shosestore.order.dao.OrderDao;
import com.ccjq.jq173.shosestore.order.po.Order;
import com.ccjq.jq173.shosestore.order.po.OrderProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {

    @Override
    public List<Order> queryAllOrders(String userId) {
        List<Order> list=new ArrayList<Order>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try
        {
            con= DbConnection.getConnection();
            ps=con.prepareStatement("select * from t_order where user_id=?");
            ps.setString(1,userId);
            rs=ps.executeQuery();
            while (rs.next())
            {
                Order o=new Order();
                o.setOrderId(rs.getString(1));
                o.setCreateTime(rs.getTimestamp(2));
                o.setPayment(rs.getInt(3));
                o.setAmount(rs.getDouble(4));
                o.setReceiver(rs.getString(5));
                o.setProvince(rs.getString(6));
                o.setCity(rs.getString(7));
                o.setCounty(rs.getString(8));
                o.setAddress(rs.getString(9));
                o.setZipcode(rs.getString(10));
                o.setTelphone(rs.getString(11));
                o.setUserId(rs.getString(12));
                list.add(o);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(rs,ps,con);
        }
        return list;
    }

    @Override
    public Order queryOneOrder(String orderId) {
        Order o=new Order();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try
        {
            con= DbConnection.getConnection();
            ps=con.prepareStatement("select * from t_order where order_id=?");
            ps.setString(1,orderId);
            rs=ps.executeQuery();
            if(rs.next())
            {
                o.setOrderId(rs.getString(1));
                o.setCreateTime(rs.getTimestamp(2));
                o.setPayment(rs.getInt(3));
                o.setAmount(rs.getDouble(4));
                o.setReceiver(rs.getString(5));
                o.setProvince(rs.getString(6));
                o.setCity(rs.getString(7));
                o.setCounty(rs.getString(8));
                o.setAddress(rs.getString(9));
                o.setZipcode(rs.getString(10));
                o.setTelphone(rs.getString(11));
                o.setUserId(rs.getString(12));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(rs,ps,con);
        }
        return o;
    }

    @Override
    public boolean addOrderProduct(String orderId, String productId, int quantity, double price,Connection con) {
        boolean b=false;
        //Connection con=null;
        PreparedStatement ps=null;
        try
        {
            //con=DbConnection.getConnection();
            ps=con.prepareStatement("insert into t_order_product values(?,?,?,?)");
            ps.setString(1,orderId);
            ps.setString(2,productId);
            ps.setInt(3,quantity);
            ps.setDouble(4,price);
            b=ps.executeUpdate()>0;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(ps);
        }
        return b;
    }

    @Override
    public boolean createOrder(Map<String, Object> order) {
        boolean b=false;
        Connection con=null;
        PreparedStatement ps=null;
        try
        {
            con=DbConnection.getConnection();
            ps=con.prepareStatement("insert into t_order values(?,systimestamp,?,?,?,?,?,?,?,?,?,?)");
            ps.setString(1,(String)order.get("orderId"));
            ps.setInt(2,(Integer)order.get("payment"));
            ps.setDouble(3,(Double)order.get("amount"));
            ps.setString(4,(String)order.get("receiver"));
            ps.setString(5,(String)order.get("province"));
            ps.setString(6,(String)order.get("city"));
            ps.setString(7,(String)order.get("county"));
            ps.setString(8,(String)order.get("address"));
            ps.setString(9,(String)order.get("zipcode"));
            ps.setString(10,(String)order.get("telphone"));
            ps.setString(11,(String)order.get("userId"));

            b=ps.executeUpdate()>0;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(null,ps,con);
        }
        return b;
    }

    @Override
    public List<OrderProduct> queryBuylog(String orderId) {
        List<OrderProduct> list=new ArrayList<OrderProduct>();

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try
        {
            con= DbConnection.getConnection();
            ps=con.prepareStatement("select * from t_order_product where order_id=?");
            ps.setString(1,orderId);
            rs=ps.executeQuery();
            while(rs.next())
            {
                OrderProduct o=new OrderProduct();
                o.setOrderId(rs.getString(1));
                o.setProductId(rs.getString(2));
                o.setQuantity(rs.getInt(3));
                o.setPrice(rs.getDouble(4));
                list.add(o);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(rs,ps,con);
        }
        return list;
    }

    @Override
    public List<Order> getPageList(int startRow, int pageSize, String userid) {
        List<Order> list=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try
        {
            con=DbConnection.getConnection();
            ps=con.prepareStatement("select * from (select * from t_order order by create_time desc) " +
                    "where order_id not in(select order_id from (select * from t_order order by create_time desc) where " +
                    "rownum<=? and user_id=?) and rownum<=? and user_id=?");
            ps.setInt(1,startRow-1);
            ps.setString(2,userid);
            ps.setInt(3,pageSize);
            ps.setString(4,userid);
            rs=ps.executeQuery();
            list=new ArrayList<Order>();
            while (rs.next())
            {
                Order o=new Order();
                o.setOrderId(rs.getString(1));
                o.setCreateTime(rs.getTimestamp(2));
                o.setPayment(rs.getInt(3));
                o.setAmount(rs.getDouble(4));
                o.setReceiver(rs.getString(5));
                o.setProvince(rs.getString(6));
                o.setCity(rs.getString(7));
                o.setCounty(rs.getString(8));
                o.setAddress(rs.getString(9));
                o.setZipcode(rs.getString(10));
                o.setTelphone(rs.getString(11));
                o.setUserId(rs.getString(12));
                list.add(o);
            }

        }
        catch(SQLException e)
        {
            e.printStackTrace();

        }
        return list;
    }

    @Override
    public int getCount(String userid) {
        int count=0;
        List<Order> list=new ArrayList<Order>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            con = DbConnection.getConnection();
            ps = con.prepareStatement("select count(order_id) from t_order where user_id=?");
            ps.setString(1, userid);
            rs = ps.executeQuery();
            rs.next();
            count=rs.getInt(1);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(con);
        }
        return count;
    }
}
